<?php
/********************************************************
 * 
 * Copyright (c) 2007 - 2008 changker.org (www.changker.org)
 * 
 * 要查看完整的版权信息和许可信息,请查看源代码中附带的 COPYRIGHT 文件,
 * 
 * 此文件的作用:
 * 
 * @copyright Copyright (c) 2007 - 2008
 * @author 刘昌功 lcg635@126.com
 * 
 ********************************************************/

defined('APP_DIR') or die('404 Not Found');

// {{{ includes
FLEA::loadClass('Common_AdminAction');
// }}}

class Controller_AdminFinancial extends Common_AdminAction {

	function __construct($ctlName)   {
		parent::__construct($ctlName);
		$this->tplDir = TPL_A;
	}
	
	function importWage() {
		if ( $this->_isPOST() ) {
			$path = $this->_checkImportedFile();
			FLEA::loadFile('Common_Excel.php');
			$data = new PHPExcel_Reader_CSV();
			$excel = $data->load($path);
			$excel->setActiveSheetIndex(0);
			$wages = $excel->getActiveSheet();
			$rows = $wages->getHighestRow();
			$columns = $wages->getHighestColumn();
			$validateColumnNames = array(
				'num'=>'学号',
				'station'=>'岗位',
				'bankCardNum'=>'银行卡号',
				'pay'=>'总工资',
				'memo'=>'备注'
			);
			$validateColumns = array();
			$wagesData = array();
			$cell = null;		
			foreach ($validateColumnNames as $k => $validateColumnName)
				for ($i = ord('A'); $i <= ord($columns); $i++) {
					$cell = $wages->getCell(chr($i) . '1');
					$columnName = iconv('gb2312', 'utf-8', $cell->getValue());
					if ( $columnName == $validateColumnName ) $validateColumns[$k] = chr($i);
				}
			
			if ( count($validateColumns) != count($validateColumnNames) ) {
				$lack = ',缺少：';
				foreach ( array_keys($validateColumnNames) as $k)
					if ( !array_key_exists($k, $validateColumns) )
						$lack .= $validateColumnNames[$k] . ',';
				js_alert('栏目数量不符合' . $lack, null, $this->_url('importWage'));
			}
			$importFailures = array();
			for ($i = 2; $i < $rows; $i++) {
				foreach ($validateColumns as $k => $validateColumn) {
					$cell = $wages->getCell($validateColumn . $i);
					$data = $cell->getValue();
					if ( $k != 'memo' && empty($data) )
						$importFailures[] = $i;
					$wagesData[$i-1][$k] = iconv('gb2312', 'utf-8', $data);
				}
			}
			if (count($importFailures) > 0)
				foreach($importFailures as $failure)
					unset($wagesData[$failure - 1]);
					
			$type = intval($_POST['type']);
			$_modWage = FLEA::getSingleton('Model_Wage');
			$result = $_modWage->import($wagesData, $_POST['month'], $type);
			
			$viewData = array(
				'columnNames' => $validateColumnNames,
				'result' => $result,
				'importFailures' => $importFailures,
				'wages' => $wagesData
			);
			
			$this->_out('financial/importResult', $viewData);
		} else {
			$viewData = array();
			$this->_out('financial/import', $viewData);
		}
	}
	
	function searchWages() {
		$_modWage = FLEA::getSingleton('Model_Wage');
		/* @var $_modWage Model_Wage */
		if ($this->_isPOST()) {
			$_modWage->search($_POST);
			redirect($this->_url('searchResult'));
		} else {
			$viewData = array('form' => $_modWage->getForm());
			$this->_out('financial/search', $viewData);
		}
	}
	
	function searchResult() {
		$_modWage = FLEA::getSingleton('Model_Wage');
		$ui = FLEA::initWebControls();
		/* @var $_modWage Model_Wage */
		$result = $_modWage->getSearchResult($this->saveGet('page'));
		$viewData = array(
			'result' => $result['lists'],
			'pagerNav' => $ui->control('pagernav', 'pagenav',
				array('pager'=>$result['pagerData'],'controller'=>'AdminFinancial', 'action'=>'searchResult'),
				true
			),
			'form' =>  $_modWage->getSearchingForm()
		);
		$this->_out('financial/search', $viewData);
	}
	
	function _checkImportedFile() {
		$files = FLEA::getSingleton('FLEA_Helper_FileUploader');
		if ( $files->existsFile('wages') && $wages = $files->getFile('wages') ) {
			if ( $wages->check('csv') ) {
				$path = dirname(APP_DIR) . '/upFiles/wages/' . date('Ym') . '.csv';
				if ( $wages->move($path) )
					return $path;
				else
					js_alert('移动文件失败', null, $this->_url('importWage'));
			}
			else
				js_alert('文件格式不正确', null, $this->_url('importWage'));
		}
		else
			js_alert('上传失败', null, $this->_url('importWage'));
	}

}